<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\admin\controller;

use app\common\model\index\BannerModel;
use app\common\model\index\CategoryModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\facade\Validate;

/**
 * 分类管理
 *
 * @class Base
 * @package app\admin\controller
 */
class Category extends Controller
{
    public $check_user    = false;
    public $check_account = false;

    /**
     * 分类管理
     * @auth true
     * @menu true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {

        CategoryModel::mQuery()->layTable(function () {
            $this->title = '分类管理';
            //$this->checkAuth();
            $this->statusArr = CategoryModel::getStatusArr();
        }, function (QueryHelper $query) {

            // 数据列表搜索过滤
            $query->like('name')->equal('status')->dateBetween('create_at');
        });
    }

    protected function _index_page_filter (&$data)
    {

        // 菜单数据树数据变平化
        $data = DataExtend::arr2table($data);

        foreach ($data as &$item) {

            if ($item['pid'] !== 0) {
                $item['name'] = 'ㅤ├ㅤ'.$item['name'];
            }
            //$item['type_name'] = $type_name[$item['type']] ?? '';

        }

    }


    /**
     * 添加分类
     *
     * @return void
     */
    public function add ()
    {
        CategoryModel::mForm();
    }

    /**
     * 编辑分类
     *
     * @return void
     */
    public function edit ()
    {
        CategoryModel::mForm();
    }

    protected function _form_filter (&$vo)
    {

        if ($this->request->isPost()) {

            $params = $this->request->post();

            if(isset($params['pid'])){
                if(empty($params['pid'])){
                    $this->error('上级分类不能为空！');
                }
            }

            $rule = [
                'name|分类名称' => 'require|length:1,100',
                'sort|排序' => 'number',
                'pid|上级分类' =>'number',
            ];
            $validate = Validate::rule($rule);
            if (!$validate->check($params)) {
                $this->error($validate->getError());
            }

        }else{
            $this->top_arr = CategoryModel::mk()->where('pid' , 0)->column('name', 'id');
        }

    }


    /**
     * 删除
     *
     * @return void
     */
    public function remove ()
    {
        CategoryModel::mDelete();
    }

    /**
     * 修改状态
     * @auth true
     */
    public function state()
    {

        CategoryModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }
}